// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import SaveManager from "../../Managers/SaveManager";
import TaskManager from "../../Models/Tasks/TaskManager";
import TaskTemplate from "../../Models/Tasks/TaskTemplate";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TaskItem extends cc.Component {

    @property(cc.Label)
    nameTxt: cc.Label = null;
    @property(cc.Label)
    describeTxt: cc.Label = null;
    @property(cc.Button)
    rewardBtn: cc.Button = null;
    @property(cc.Label)
    rewardNumTxt: cc.Label = null;

    taskTp: TaskTemplate = null;
    taskArr: TaskTemplate[] = null;
    nestRewardIndex: number = 0;
    drawItem(tps: TaskTemplate[]) {
        this.taskTp = tps[0];
        this.taskArr = tps;
        var taskEy = TaskManager.single.getTaskEntity(this.taskTp.taskType);
        if (taskEy != null) {
            this.nestRewardIndex = taskEy.nestRewardIndex;
            var tp = this.taskArr[taskEy.nestRewardIndex];
            this.describeTxt.string = this.taskTp.taskDescribe.replace("@1", taskEy.taskPregress + "/" + tp.targetNum);
            this.rewardNumTxt.string = "" + tp.diamondNum;

            var arr = TaskManager.single.getTaskListByType(this.taskTp.taskType);
            this.rewardBtn.interactable = !taskEy.rewards[this.nestRewardIndex] && taskEy.taskPregress >= arr[this.nestRewardIndex].targetNum;

        } else {
            this.describeTxt.string = this.taskTp.taskDescribe.replace("@1", "0/" + this.taskTp.targetNum);
            this.rewardNumTxt.string = "" + this.taskTp.diamondNum;
            this.rewardBtn.interactable = false;
        }
        this.nameTxt.string = this.taskTp.taskName;
    }

    rewardClick() {
        TaskManager.single.requestReceiveTaskReward(this.taskTp.taskType, this.nestRewardIndex);
    }
}
